package com.sqly.doc;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;

import com.blankj.utilcode.util.PermissionUtils;
import com.sqly.doc.user.user_login;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;

/**
 * 欢迎界面
 */
@RuntimePermissions
public class WelcomeActivity extends Activity {

    private static final String[] RUNTIME_PERMISSION  = {Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_PHONE_STATE};
    private static final int ASK_PERMISSION_REQUESTCODE = 891000;

    Context con = WelcomeActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        WelcomeActivityPermissionsDispatcher.checkAuthWithPermissionCheck(this);

    }

    public void toLoginActivity() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(con, user_login.class));
                finish();
            }
        }, 2000);
    }

    @NeedsPermission({Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_PHONE_STATE})
    public void checkAuth() {

        toLoginActivity();

    }


    @OnShowRationale({Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_PHONE_STATE})
    public void showRationaleForAuth(final PermissionRequest request) {
        request.proceed();
    }

    @OnPermissionDenied({Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_PHONE_STATE})
    public void showDeniedForAuth() {
        WelcomeActivityPermissionsDispatcher.checkAuthWithPermissionCheck(this);
    }

    @OnNeverAskAgain({Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.RECORD_AUDIO,Manifest.permission.READ_PHONE_STATE})
    public void showNeverAskForAuth() {
        StringBuilder tipMsgBuilder = new StringBuilder();
        for (String permission : RUNTIME_PERMISSION) {
            if (!PermissionUtils.isGranted(permission)) {
                switch (permission){
                    case Manifest.permission.CAMERA :
                        tipMsgBuilder.append("拍摄照片和录制视频权限").append("\n");
                        break;
                    case Manifest.permission.READ_EXTERNAL_STORAGE :
                        tipMsgBuilder.append("访问设备上的照片、媒体内容和文件权限").append("\n");
                        break;
                    case Manifest.permission.WRITE_EXTERNAL_STORAGE :
                        tipMsgBuilder.append("存储权限").append("\n");
                        break;
                    case Manifest.permission.RECORD_AUDIO :
                        tipMsgBuilder.append("录制音频权限").append("\n");
                        break;
                    case Manifest.permission.READ_PHONE_STATE :
                        tipMsgBuilder.append("获取设备信息权限").append("\n");
                        break;
                }
            }
        }
        String tipMsg = tipMsgBuilder.toString();
        tipMsg = tipMsg.substring(0,tipMsg.lastIndexOf("\n"));
//        Dialog dialog = new Dialog(this,"信息提示",tipMsg,false) {
//            @Override
//            public void callCancel() {
//                WelcomeActivityPermissionsDispatcher.checkAuthWithPermissionCheck(WelcomeActivity.this);
//            }
//            @Override
//            public void callConfirm() {
//                Intent mIntent = new Intent();
//                if (Build.VERSION.SDK_INT >= 9) {
//                    mIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
//                    mIntent.setData(Uri.fromParts("package", getPackageName(), null));
//                } else if (Build.VERSION.SDK_INT <= 8) {
//                    mIntent.setAction(Intent.ACTION_VIEW);
//                    mIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
//                    mIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
//                }
//                startActivityForResult(mIntent,ASK_PERMISSION_REQUESTCODE);
//            }
//        };
//        dialog.setSubTitleViewText("APP运行需要以下权限，请前往设置并授权");
//        dialog.setConfirmBtnText("前往设置");
//        dialog.setMsgTextSize(12);
//        dialog.setOnlyConfirmBtn();
//        dialog.show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(ASK_PERMISSION_REQUESTCODE == requestCode){
            WelcomeActivityPermissionsDispatcher.checkAuthWithPermissionCheck(this);
        }
    }

    @SuppressLint("NeedOnRequestPermissionsResult")
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        WelcomeActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

}
